<template>
  <div class="login-background-container">
    <img src="@/assets/svg/login/background.svg" alt="Background" class="login-wave" />
  </div>
  <div class="login-page">
    <div class="login-logo-container">
      <h2>AIToEdu</h2>
      <img src="@/assets/logo.png" alt="Logo" class="login-logo" />
    </div>
    <div class="login-container">
      <div class="login-illustration">
        <img src="@/assets/svg/login/pic1.svg" alt="Illustration" />
      </div>
      <div class="login-form-container">
        <h2>登录</h2>
        <form @submit.prevent="handleSubmit">
          <div class="login-input-group">
            <el-input size="large" prefix-icon="Message" type="email" placeholder="电子邮箱" v-model="email" required />
          </div>
          <div class="login-input-group">
            <el-input size="large" prefix-icon="Lock" type="password" show-password="true" placeholder="密码" v-model="password" required />
            <a href="#" class="login-forgot-password">忘记密码?</a>
          </div>
          <button type="submit" class="login-button">登录</button>
        </form>
        <p class="login-register-text">没有账号，立即注册?</p>
        <button @click="register" class="login-register-button">注册</button>
      </div>
    </div>
  </div>
</template>
<script setup name="LoginView" lang="ts">
import { ref } from 'vue';
import router from '@/router'
import axios from 'axios'
import { ElMessage } from 'element-plus';

const email = ref('')
const password = ref('')

const handleSubmit = async () => {
  console.log('Email:', email.value)
  console.log('Password:', password.value)
  let role = -1;
  await axios.post('logic/user/login/email', { email: email.value, password: password.value })
    .then(res => {
      console.log(res.data)
      if(res.data.code === 200) {
        console.log(res.data.data.satoken);
        window.sessionStorage.setItem('satoken', res.data.data.satoken)
        ElMessage({
          message: '登录成功！',
          type: 'success'
        })
      } else {
        ElMessage({
          message: '账号或者密码错误！',
          type: 'error'
        })
      }
    })
  await axios.get('logic/user/role')
    .then(res => {
      role = res.data.data;
      console.log(role)
    })
  if (role === 1) {
    router.push('/student');
  }
}

const register = () => {
  console.log('注册')
  router.push('/register')
}

</script>
<style>
html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
}

.login-logo-container {
  display: flex;
  position: absolute;
  left: 20px;
  top: -15px;
  align-items: center;
  gap: 10px;
  z-index: 1;
}

.login-logo-container h2 {
  font-size: 55px;
  color: #dfefff !important;
}

.login-logo {
  width: 70px;
}

.login-page {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
}

.login-background-container {
  position: absolute;
  top: 0;
  left: -960px;
  width: 100%;
  height: 100%;
  background: white;
  z-index: -1;
  backdrop-filter: blur(20px);
}

.login-wave {
  position: absolute;
  bottom: 0;
  width: 100%;
}

.login-container {
  display: flex;
  width: 1200px;
  height: 720px;
  background: rgba(255, 255, 255, 0.042);
  border-radius: 16px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(20px);
}

.login-container::after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  width: 80%;
  height: 100%;
  z-index: -1;
}

.login-illustration {
  width: 700px;
  padding: 40px 0;
  border-radius: 16px 0 0 16px;
  background-color: #f4f8fb;
  display: flex;
  justify-content: center;
}

.login-illustration img {
  max-width: 500px;
  height: auto;
}

.login-form-container {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.login-container h2 {
  font-size: 40px;
  font-weight: lighter;
  margin-top: -50px;
  margin-bottom: 70px;
}

.login-input-group {
  position: relative;
  margin-bottom: 20px;
}

.login-input-group input {
  width: 350px;
  font-size: 16px;
}

.login-forgot-password {
  position: absolute;
  right: 10px;
  bottom: -25px;
  font-size: 12px;
  color: #007BFF;
  text-decoration: none;
}

.login-button,
.login-register-button {
  width: 100%;
  padding: 10px;
  font-size: 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 10px;
}

.login-button {
  background: #3A8FE6;
  color: white;
  border-radius: 24px;
  margin-top: 30px;
}

.login-register-button {
  width: 424px;
  background: none;
  color: #007BFF;
  border: 1px solid #007BFF;
  border-radius: 24px;
}

.login-register-text {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #828282;
}

@media (max-width: 768px) {
  .login-container {
    width: 90%;
    flex-direction: column;
    border-radius: 0;
  }
  .login-illustration,
  .login-form-container {
    width: 100%;
  }
}
</style>
